<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('procure.purchorders.orderno')" type="string" v-model="baTable.form.items!.orderno" prop="orderno" :placeholder="t('Please input field', { field: t('procure.purchorders.orderno') })" />
                    <FormItem :label="t('procure.purchorders.supplierno')" type="remoteSelect" v-model="baTable.form.items!.supplierno" prop="supplierno" :input-attr="{ pk: 'cncerp_procure_suppliers.supplierid', field: 'suppname', 'remote-url': '/admin/procure.Suppliers/index' }" :placeholder="t('Please select field', { field: t('procure.purchorders.supplierno') })" />
                    <FormItem :label="t('procure.purchorders.comments')" type="string" v-model="baTable.form.items!.comments" prop="comments" :placeholder="t('Please input field', { field: t('procure.purchorders.comments') })" />
                    <FormItem :label="t('procure.purchorders.orddate')" type="datetime" v-model="baTable.form.items!.orddate" prop="orddate" :placeholder="t('Please select field', { field: t('procure.purchorders.orddate') })" />
                    <FormItem :label="t('procure.purchorders.rate')" type="number" prop="rate" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.rate" :placeholder="t('Please input field', { field: t('procure.purchorders.rate') })" />
                    <FormItem :label="t('procure.purchorders.dateprinted')" type="datetime" v-model="baTable.form.items!.dateprinted" prop="dateprinted" :placeholder="t('Please select field', { field: t('procure.purchorders.dateprinted') })" />
                    <FormItem :label="t('procure.purchorders.allowprint')" type="number" prop="allowprint" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.allowprint" :placeholder="t('Please input field', { field: t('procure.purchorders.allowprint') })" />
                    <FormItem :label="t('procure.purchorders.initiator')" type="remoteSelect" v-model="baTable.form.items!.initiator" prop="initiator" :input-attr="{ pk: 'cncerp_admin.id', field: 'username', 'remote-url': '/admin/auth.Admin/index' }" :placeholder="t('Please select field', { field: t('procure.purchorders.initiator') })" />
                    <FormItem :label="t('procure.purchorders.requisitionno')" type="string" v-model="baTable.form.items!.requisitionno" prop="requisitionno" :placeholder="t('Please input field', { field: t('procure.purchorders.requisitionno') })" />
                    <FormItem :label="t('procure.purchorders.intostocklocation')" type="remoteSelect" v-model="baTable.form.items!.intostocklocation" prop="intostocklocation" :input-attr="{ pk: 'cncerp_stock_locations.loccode', field: 'locationname', 'remote-url': '/admin/stock.Locations/index' }" :placeholder="t('Please select field', { field: t('procure.purchorders.intostocklocation') })" />
                    <FormItem :label="t('procure.purchorders.deladd1')" type="string" v-model="baTable.form.items!.deladd1" prop="deladd1" :placeholder="t('Please input field', { field: t('procure.purchorders.deladd1') })" />
                    <FormItem :label="t('procure.purchorders.deladd2')" type="string" v-model="baTable.form.items!.deladd2" prop="deladd2" :placeholder="t('Please input field', { field: t('procure.purchorders.deladd2') })" />
                    <FormItem :label="t('procure.purchorders.deladd3')" type="string" v-model="baTable.form.items!.deladd3" prop="deladd3" :placeholder="t('Please input field', { field: t('procure.purchorders.deladd3') })" />
                    <FormItem :label="t('procure.purchorders.deladd4')" type="string" v-model="baTable.form.items!.deladd4" prop="deladd4" :placeholder="t('Please input field', { field: t('procure.purchorders.deladd4') })" />
                    <FormItem :label="t('procure.purchorders.deladd5')" type="string" v-model="baTable.form.items!.deladd5" prop="deladd5" :placeholder="t('Please input field', { field: t('procure.purchorders.deladd5') })" />
                    <FormItem :label="t('procure.purchorders.deladd6')" type="string" v-model="baTable.form.items!.deladd6" prop="deladd6" :placeholder="t('Please input field', { field: t('procure.purchorders.deladd6') })" />
                    <FormItem :label="t('procure.purchorders.tel')" type="string" v-model="baTable.form.items!.tel" prop="tel" :placeholder="t('Please input field', { field: t('procure.purchorders.tel') })" />
                    <FormItem :label="t('procure.purchorders.suppdeladdress1')" type="string" v-model="baTable.form.items!.suppdeladdress1" prop="suppdeladdress1" :placeholder="t('Please input field', { field: t('procure.purchorders.suppdeladdress1') })" />
                    <FormItem :label="t('procure.purchorders.suppdeladdress2')" type="string" v-model="baTable.form.items!.suppdeladdress2" prop="suppdeladdress2" :placeholder="t('Please input field', { field: t('procure.purchorders.suppdeladdress2') })" />
                    <FormItem :label="t('procure.purchorders.suppdeladdress3')" type="string" v-model="baTable.form.items!.suppdeladdress3" prop="suppdeladdress3" :placeholder="t('Please input field', { field: t('procure.purchorders.suppdeladdress3') })" />
                    <FormItem :label="t('procure.purchorders.suppdeladdress4')" type="string" v-model="baTable.form.items!.suppdeladdress4" prop="suppdeladdress4" :placeholder="t('Please input field', { field: t('procure.purchorders.suppdeladdress4') })" />
                    <FormItem :label="t('procure.purchorders.suppdeladdress5')" type="string" v-model="baTable.form.items!.suppdeladdress5" prop="suppdeladdress5" :placeholder="t('Please input field', { field: t('procure.purchorders.suppdeladdress5') })" />
                    <FormItem :label="t('procure.purchorders.suppdeladdress6')" type="string" v-model="baTable.form.items!.suppdeladdress6" prop="suppdeladdress6" :placeholder="t('Please input field', { field: t('procure.purchorders.suppdeladdress6') })" />
                    <FormItem :label="t('procure.purchorders.suppliercontact')" type="string" v-model="baTable.form.items!.suppliercontact" prop="suppliercontact" :placeholder="t('Please input field', { field: t('procure.purchorders.suppliercontact') })" />
                    <FormItem :label="t('procure.purchorders.supptel')" type="string" v-model="baTable.form.items!.supptel" prop="supptel" :placeholder="t('Please input field', { field: t('procure.purchorders.supptel') })" />
                    <FormItem :label="t('procure.purchorders.contact')" type="string" v-model="baTable.form.items!.contact" prop="contact" :placeholder="t('Please input field', { field: t('procure.purchorders.contact') })" />
                    <FormItem :label="t('procure.purchorders.version')" type="number" prop="version" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.version" :placeholder="t('Please input field', { field: t('procure.purchorders.version') })" />
                    <FormItem :label="t('procure.purchorders.revised')" type="date" v-model="baTable.form.items!.revised" prop="revised" :placeholder="t('Please select field', { field: t('procure.purchorders.revised') })" />
                    <FormItem :label="t('procure.purchorders.realorderno')" type="string" v-model="baTable.form.items!.realorderno" prop="realorderno" :placeholder="t('Please input field', { field: t('procure.purchorders.realorderno') })" />
                    <FormItem :label="t('procure.purchorders.deliveryby')" type="string" v-model="baTable.form.items!.deliveryby" prop="deliveryby" :placeholder="t('Please input field', { field: t('procure.purchorders.deliveryby') })" />
                    <FormItem :label="t('procure.purchorders.deliverydate')" type="date" v-model="baTable.form.items!.deliverydate" prop="deliverydate" :placeholder="t('Please select field', { field: t('procure.purchorders.deliverydate') })" />
                    <FormItem :label="t('procure.purchorders.status')" type="radio" v-model="baTable.form.items!.status" prop="status" :data="{ content: { Completed: t('procure.purchorders.status Completed'), Rejected: t('procure.purchorders.status Rejected'), Pending: t('procure.purchorders.status Pending'), Authorised: t('procure.purchorders.status Authorised') } }" :placeholder="t('Please select field', { field: t('procure.purchorders.status') })" />
                    <FormItem :label="t('procure.purchorders.stat_comment')" type="textarea" v-model="baTable.form.items!.stat_comment" prop="stat_comment" :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('procure.purchorders.stat_comment') })" />
                    <FormItem :label="t('procure.purchorders.paymentterms')" type="remoteSelect" v-model="baTable.form.items!.paymentterms" prop="paymentterms" :input-attr="{ pk: 'cncerp_sales_paymentterms.termsindicator', field: 'terms', 'remote-url': '/admin/sales.Paymentterms/index' }" :placeholder="t('Please select field', { field: t('procure.purchorders.paymentterms') })" />
                    <FormItem :label="t('procure.purchorders.port')" type="string" v-model="baTable.form.items!.port" prop="port" :placeholder="t('Please input field', { field: t('procure.purchorders.port') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    orddate: [buildValidatorData({ name: 'date', title: t('procure.purchorders.orddate') })],
    rate: [buildValidatorData({ name: 'number', title: t('procure.purchorders.rate') })],
    dateprinted: [buildValidatorData({ name: 'date', title: t('procure.purchorders.dateprinted') })],
    initiator: [buildValidatorData({ name: 'required', title: t('procure.purchorders.initiator') })],
    tel: [buildValidatorData({ name: 'mobile', title: t('procure.purchorders.tel') })],
    supptel: [buildValidatorData({ name: 'mobile', title: t('procure.purchorders.supptel') })],
    version: [buildValidatorData({ name: 'number', title: t('procure.purchorders.version') })],
    revised: [buildValidatorData({ name: 'date', title: t('procure.purchorders.revised') })],
    deliverydate: [buildValidatorData({ name: 'date', title: t('procure.purchorders.deliverydate') })],
})
</script>

<style scoped lang="scss"></style>
